package com.easyapi.web.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyapi.common.contants.Constants;
import com.easyapi.common.enums.StatusCode;
import com.easyapi.common.exception.BizException;
import com.easyapi.common.param.api.ApiInfoPageParam;
import com.easyapi.common.param.api.ApiOperationParam;
import com.easyapi.common.param.api.ConfigSqlApiParam;
import com.easyapi.common.param.api.GenerateApiParam;
import com.easyapi.common.utils.BaseResponse;
import com.easyapi.common.utils.RegularUtil;
import com.easyapi.common.utils.ResultUtils;
import com.easyapi.common.vo.api.ApiInfoPageVo;
import com.easyapi.common.vo.api.ApiInfoVo;
import com.easyapi.common.vo.api.ApiMarketInfo;
import com.easyapi.server.service.api.ApiService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author javadadi
 */
@RestController
@RequestMapping(Constants.BASE_URL + "interface")
@Api(tags = {"EApiManage"})
public class ApiController {
    @Resource
    private ApiService apiService;

    @PostMapping("/pageInterfaceInfo")
    public BaseResponse<Page<ApiInfoPageVo>> pageInterfaceInfo(@RequestBody ApiInfoPageParam param) {
        Long id = param.getId();
        String apiNameCn = param.getApiNameCn();
        Long projectId = param.getProjectId();
        Long datasourceId = param.getDatasourceId();
        if (RegularUtil.StrsLengthError(1, 255, apiNameCn)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (RegularUtil.longsError(0L, id, projectId, datasourceId)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        return apiService.pageApiInfo(param);
    }

    @PostMapping("/generateInterface")
    public BaseResponse<Boolean> generateInterface(@RequestBody GenerateApiParam param) {
        Long projectId = param.getProjectId();
        String apiNameCn = param.getApiNameCn();
        String apiName = param.getApiName();
        Integer reqType = param.getReqType();
        Integer queryTimeOut = param.getQueryTimeOut();
        Integer reqLimit = param.getReqLimit();
        Integer isCache = param.getIsCache();
        Long datasourceId = param.getDatasourceId();
        String databaseName = param.getDatabaseName();
        String tableName = param.getTableName();
        //数值类型校验
        if (Objects.isNull(projectId) || projectId <= 0 || Objects.isNull(datasourceId) || datasourceId <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        //字符型校验
        if (StringUtils.isAnyBlank(apiName, apiNameCn, databaseName, tableName)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (Objects.isNull(reqType) || Objects.isNull(queryTimeOut) || Objects.isNull(reqLimit) || Objects.isNull(isCache)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        return apiService.generateInterface(param);
    }

    @PostMapping("configInterface")
    public BaseResponse<Boolean> configInterface(@RequestBody ConfigSqlApiParam param) {
        Long projectId = param.getProjectId();
        String apiNameCn = param.getApiNameCn();
        String apiName = param.getApiName();
        Integer reqType = param.getReqType();
        Integer queryTimeOut = param.getQueryTimeOut();
        Integer reqLimit = param.getReqLimit();
        Integer isCache = param.getIsCache();
        Long datasourceId = param.getDatasourceId();
        String databaseName = param.getDatabaseName();
        String configSql = param.getConfigSql();
        if (StringUtils.isEmpty(configSql)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        //数值类型校验
        if (Objects.isNull(projectId) || projectId <= 0 || Objects.isNull(datasourceId) || datasourceId <= 0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        //字符型校验
        if (StringUtils.isAnyBlank(apiName, apiNameCn, databaseName)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (Objects.isNull(reqType) || Objects.isNull(queryTimeOut) || Objects.isNull(reqLimit) || Objects.isNull(isCache)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        return apiService.configSqlInterface(param);
    }

    @PostMapping("/marketInterfaceInfo")
    public BaseResponse<Page<ApiMarketInfo>> getMarketInterfaceInfo(@RequestBody ApiInfoPageParam param) {
        Long id = param.getId();
        String apiNameCn = param.getApiNameCn();
        Long projectId = param.getProjectId();
        Long datasourceId = param.getDatasourceId();
        if (RegularUtil.StrsLengthError(1, 255, apiNameCn)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        if (RegularUtil.longsError(0L, id, projectId,datasourceId)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        return apiService.getApiMarketInfo(param);
    }

    @PostMapping("/getInterfaceInfo")
    public BaseResponse<ApiInfoVo> getInterfaceInfo(@RequestBody ApiInfoPageParam param) {
        Long id = param.getId();
        if (Objects.isNull(id)) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        return apiService.getInterfaceInfo(id);
    }
    @PostMapping("interfaceOperation")
    public BaseResponse<Boolean> interfaceOperation(@RequestBody ApiOperationParam param) {
        if (StringUtils.isEmpty(param.getAction()) || Objects.isNull(param.getId()) || param.getId()<=0) {
            throw new BizException(StatusCode.PARAMS_ERROR);
        }
        apiService.apiOperation(param.getId(),param.getAction());
        return ResultUtils.success(true);
    }
}
